//游戏主逻辑
cc.Class({
    extends: cc.Component,

    properties: {
        //存放所有学生节点的数组
        studentArray:{
            default: [],
            type: [cc.Node]
        },
        //飞机允许在学生身上停留的最长时间
        maxTime: {
            default: 0,
            type: Number
        },
        //计时器
        timer: {
            default: 0,
            type: Number
        },
        //纸飞机实体
        paperEntity: {
            default: null,
            type: cc.Node
        },
        //纸飞机是否发出的标志位
        paperHasChanged: {
            default: false,
            type: Boolean
        },
        //记录当前有纸飞机的学生索引
        currentPaperStudentIndex: {
            default: 1,
            type: Number
        },
        //倒数计时器的节点
        timerNode: {
            default: null,
            type: cc.Node
        }
    },

    onLoad () {
        //游戏开始时默认将纸飞机放在一号学生手中
        this.setPaperToStudent(1);
        //游戏开始时默认5号学生需要纸飞机
        this.setStudentInNeed(5);
        //纸飞机在学生身上时隐藏纸飞机
        this.paperEntity.active = false;
    },

    start () {

    },

    update (dt) {
        //计时，若飞机在一个学生身上停留时间过长则Game Over
        //时刻判断纸飞机发出标志位是否改变为true
        if (!this.paperHasChanged) {
            //没有改变则正常计时
            this.timer = this.timer + dt;
            if (this.timer > this.maxTime) {
                this.gameOver();
            }
        }else {
            //纸飞机发出标志位被改变为true时，计时清零，并且重新选择需要纸飞机的学生
            this.timer = 0;
            this.setRandomStudentWhoNeedPaper();
            this.paperHasChanged = false;
            this.timerNode.getComponent("Timer").timer = 5;
        }

    },

    //将纸飞机放在指定的学生手上
    setPaperToStudent (number) {
        for (let i = 0; i < this.studentArray.length; i++) {
            if (i === (number - 1)) {
                this.studentArray[i].getComponent("Student").hasPaper = true;
            }else {
                this.studentArray[i].getComponent("Student").hasPaper = false;
            }
        }
    },

    //将指定学生设置为需要纸飞机的状态
    setStudentInNeed (number) {
        for (let i = 0; i < this.studentArray.length; i++) {
            if (i === (number - 1)) {
                this.studentArray[i].getComponent("Student").needPaper = true;
            }else {
                this.studentArray[i].getComponent("Student").needPaper = false;
            }
        }
    },

    //随机数决定需要纸飞机的学生（排除已经拥有纸飞机的学生）
    setRandomStudentWhoNeedPaper () {
        let randomNumber = Math.floor(Math.random() * 12);
        let hasStudentIndex = 0;
        for (let i = 0; i < this.studentArray.length; i++) {
            if (this.studentArray[i].getComponent("Student").hasPaper === true) {
                hasStudentIndex = i;
                console.log("hasStudentIndex:" + hasStudentIndex);
            }
        }
        while (randomNumber === hasStudentIndex) {
            randomNumber = Math.floor(Math.random() * 12);
        }
        this.setStudentInNeed(randomNumber + 1);
        console.log("当前需要纸飞机的学生：" + (randomNumber + 1));
        console.log("当前有纸飞机的学生：" + (hasStudentIndex + 1));
    },

    //游戏失败
    gameOver () {
        this.timer = 0;
        cc.director.loadScene("main_scene");
    },
});
